package com.hrpmar.controller;


import com.hrpmar.pojo.Worker;
import com.hrpmar.service.LoginService;
import com.hrpmar.utils.Constants;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by c on 2017-11-14.
 */
@RestController
public class LoginController {

    String msg=null;

    @Resource
    private LoginService loginService;

    @RequestMapping("/userlogin")
    public String login(Integer deId, String landCode, String landPwd, String rememberMe, HttpSession session, HttpServletResponse response){
        Worker worker =null;
        worker = loginService.login(deId,landCode,landPwd);
        if(worker==null){
            msg="登录名或密码错误!";
        } else {
            msg=null;
            session.setAttribute(Constants.LOGIN_USER,worker);
            if(rememberMe!=null){
                Cookie cname = new Cookie("name",worker.getLandCode());
                Cookie cpwd = new Cookie("pwd", worker.getLandPwd());
                Cookie cid = new Cookie("id",deId.toString());
                cname.setMaxAge(60*60*24);
                cpwd.setMaxAge(60*60*24);
                cid.setMaxAge(60*60*24);
                response.addCookie(cname);
                response.addCookie(cpwd);
                response.addCookie(cid);
            }
        }
        return msg;
    }

}
